package com.tdh.controller;

import com.tdh.pojo.TDepart;
import com.tdh.pojo.TUser;
import com.tdh.service.DepartService;
import com.tdh.service.TsBzdmService;
import com.tdh.service.UserService;
import com.tdh.utils.LoadXml;
import com.tdh.utils.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.List;

@Controller
public class UserController {

    @Resource
    private UserService userService;

    @Resource
    private DepartService departService;

    @Resource
    private TsBzdmService tsBzdmService;

    /**
     * 登录验证，查询yhid和yhkl
     * @param yhid 账号
     * @param yhkl 密码
     * @return 执行页面跳转
     */
    @RequestMapping(value = "/doLogin", method = RequestMethod.GET)
    public String doLogin(@RequestParam(value="userId") String yhid, @RequestParam(value="password") String yhkl) {
        List<TUser> userList = userService.selectLoginUser(yhid, yhkl);
        if(userList.size() > 0) {
            return "/user";
        } else {
            return "redirect:/login.jsp";
        }
    }

    /**
     * 根据id删除用户
     * @param userId 用户id
     */
    @RequestMapping(value = "/deleteUser")
    @ResponseBody
    public void deleteUser(@RequestParam(value = "userId") String userId) {
        userService.deleteUser(userId);
    }

    /**
     * 更新用户信息
     * @param user TUser对象
     */
    @RequestMapping(value = "/updateUser")
    @ResponseBody
    public void updateUser(@RequestBody TUser user) {
        userService.updateUser(user);
    }

    /**
     * 插入用户
     * @param user TUser对象
     */
    @RequestMapping(value = "/insertUser")
    @ResponseBody
    public void insertUser(@RequestBody TUser user) {
        userService.insertUser(user);
    }

    /**
     * 根据用户id、姓名、部门 分页查询
     * @param user 用户id或姓名
     * @param depart 部门代码
     * @param start 数据库起始行
     * @param limit 每页条数
     * @return 响应体：xml字符串
     */
    @RequestMapping(value = "/queryUser", produces = {"application/xml;charset=UTF-8"})
    @ResponseBody
    public String queryUserList(@RequestParam("user") String user, @RequestParam("depart") String depart,
                                @RequestParam("start") int start, @RequestParam("limit") int limit) {

        List<TUser> tUserList = userService.selectUserTableLimit(StringUtil.trim(user), StringUtil.trim(depart), start - 1, limit);
        String totalNumber = userService.countRows(user, depart);
        return new LoadXml().getXML(tUserList, totalNumber);
    }

    /**
     * 显示user.jsp 并加载部门下拉框
     * @return modelAndView
     */
    @RequestMapping("/user")
    public ModelAndView showUserPage() {

        ModelAndView mav = new ModelAndView("view/user");

        //查询部门选项
        List<TDepart> departs = departService.getDeparts();

        mav.addObject("departOption", departs);

        return mav;
    }

    /**
     * 打开查看或修改用户信息页
     * @param type 操作类型 打开 | 修改
     * @param userId 用户id
     * @return user/table.jsp
     */
    @RequestMapping("/viewOrEdit")
    public ModelAndView showTablePage(@RequestParam("type") String type, @RequestParam("userId") String userId) {
        ModelAndView mav = new ModelAndView("view/table");

        mav.addObject("departOption", departService.getDeparts());
        mav.addObject("sexOption", tsBzdmService.getTSexes());
        mav.addObject("user", userService.queryById(userId));
        mav.addObject("type", type);

        return mav;
    }

    /**
     * 打开新增窗口
     * @param type String url中type
     * @return user/table.jsp
     */
    @RequestMapping("/addPage")
    public ModelAndView showAddPage(@RequestParam("type") String type) {
        ModelAndView mav = new ModelAndView("view/table");

        mav.addObject("departOption", departService.getDeparts());
        mav.addObject("sexOption", tsBzdmService.getTSexes());
        mav.addObject("maxPxh", userService.queryPxh());
        mav.addObject("type", type);

        return  mav;
    }
}
